﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class RegisterAccount : System.Web.UI.Page
{
    private SqlConnection connect;
    private SqlCommand command;
    private string connectionString;

    string connectionStringBank;
    private SqlConnection conBank;

    DataSet ds;
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["myCon"].ToString();
        connect = new SqlConnection(connectionString);
        connect.Open();

        

    }
    protected void getAddNewUser(string username, string pass, string full, string DOB, string sex, string email, int phone, string address)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }

            command = new SqlCommand("spAddNewAccount", connect);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@UserName", SqlDbType.VarChar).Value = username;
            command.Parameters.Add("@Password ", SqlDbType.VarChar).Value = pass;
            command.Parameters.Add("@CustomerName", SqlDbType.NText).Value = full;
            command.Parameters.Add("@DoB", SqlDbType.DateTime).Value = DOB;
            command.Parameters.Add("@Gender", SqlDbType.VarChar).Value = sex;
            command.Parameters.Add("@Email", SqlDbType.VarChar).Value = email;
            command.Parameters.Add("@PhoneNumber", SqlDbType.Int).Value = phone;
            command.Parameters.Add("@Address", SqlDbType.NText).Value = address;
            command.ExecuteNonQuery();
            lblStatus.Text = "Register successfull";
            
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }
    protected void getAddNewUserBank(string username)
    {
        try
        {
            connectionStringBank = WebConfigurationManager.ConnectionStrings["myConBank"].ToString();
            conBank = new SqlConnection(connectionStringBank);
            conBank.Open();

            if (conBank.State == ConnectionState.Closed)
            {
                conBank.Open();
            }

            command = new SqlCommand("spInsertNewAccount", conBank);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
            command.ExecuteNonQuery();
         
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conBank.Close();
        }
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        string user, pass, fullname, DOB = "", sex, email, add;
        int phone;
        user = txtUserName.Text;
        pass = txtPassword.Text;
        fullname = txtFullName.Text;
        if (RaMale.Checked == true)
        {
            sex = "Male";
        }
        else
        {
            sex = "Female";
        }
       // DOB = txtDOB.Text;
        DOB = Request["datepicker"];
        add = txtAddress.Text;
        phone = Int32.Parse(txtPhone.Text);
        email = txtEmail.Text;
        lblStatus.Text = DOB;

        checkUser(user, pass, fullname, DOB, sex, email, phone, add);

        Response.Redirect("Default.aspx");
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        {
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtFullName.Text = "";
           // txtDOB.Text = "";
            txtAddress.Text = "";
            txtPhone.Text = "";
            txtEmail.Text = "";
        }
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
       // txtDOB.Text = Calendar1.SelectedDate.ToString();
    }

    protected void checkUser(string username, string pass, string full, string dob, string sex, string email, int phone, string add)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            command = new SqlCommand("spGetAccount", connect);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@userName", SqlDbType.VarChar).Value = username;
            command.ExecuteNonQuery();
            da = new SqlDataAdapter(command);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count == 0)
            {
               getAddNewUser(username, pass, full, dob, sex, email, phone, add);
               getAddNewUserBank(username);
            }
            else if (ds.Tables[0].Rows.Count != 0)
            {
                lblStatus.Text = "UserName had been used";
            }
            
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }
}